package br.ufmg.notservice.utils;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import br.ufmg.notservice.R;

public class AndroidUtils {

	private static int id = 1;

	public static void runTask(Runnable task, long delay) {
		Handler handler = new Handler();
		handler.removeCallbacks(task);
		handler.postDelayed(task, delay);
	}

	public static void showNotification(android.app.Activity activity,
			String title, String text, String origin) {
		
		NotificationManager notManager = (NotificationManager) activity
				.getSystemService(Context.NOTIFICATION_SERVICE);
		long time = System.currentTimeMillis();

		Notification not = new Notification(R.drawable.icon, text, time);

		PendingIntent contentIntent = PendingIntent.getActivity(activity, 0,
				new Intent(activity, activity.getClass()), 0);

		// Set the info for the views that show in the notification panel.
		not.setLatestEventInfo(activity, title, text, contentIntent);

		notManager.notify(id, not);
		id++;
		if (id == Integer.MAX_VALUE) {
			id = 1;
		}

	}

	public static void sleepThread(int ms) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void startActivity(Activity activity, Class clazz) {
		Intent i = new Intent(activity.getApplicationContext(), clazz);
		activity.startActivity(i);
	}

	public static void showAlert(Activity activity, String message) {
		showAlert(activity.getApplicationContext(), message);
	}

	public static void notifyError(Exception e, Activity activity, String op) {
		AndroidUtils.showAlert(activity, "An error ocurred.");
		Log.e(op, e.getMessage());
	}

	public static void notifyError(Exception e, Context context, String op,
			String source) {
		AndroidUtils.showAlert(context, "An error ocurred.");
		Log.e(op, e.getMessage());
	}

	public static void notifyError(String message, Activity activity, String op) {
		AndroidUtils.showAlert(activity, "An error ocurred: " + message);
		Log.e(op, message);
	}

	public static void showAlert(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	public static String[] getStringArray(Activity activity, int id) {
		return activity.getResources().getStringArray(id);
	}

	public static String getConnectivityInfo(Activity activity) {
		ConnectivityManager conMan = (ConnectivityManager) activity
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);

		// 0 mobile
		// 1 wifi
		NetworkInfo activeNetworkInfo = conMan.getActiveNetworkInfo();
		String conType = activeNetworkInfo.getTypeName();
		String state = activeNetworkInfo.getDetailedState().toString();
		return conType + " : " + state;
	}

	public static String getConnectivityName(Activity activity) {
		ConnectivityManager conMan = (ConnectivityManager) activity
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);

		// 0 mobile
		// 1 wifi
		NetworkInfo activeNetworkInfo = conMan.getActiveNetworkInfo();
		return activeNetworkInfo.getTypeName();
	}

	public static boolean checkPermission(Context ctx, String permission) {
		return ctx.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED;
	}

	public static ArrayAdapter<String> createAdapter(Activity activity,
			int arrayId) {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
				android.R.layout.simple_spinner_item, activity.getResources()
						.getStringArray(arrayId));
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return adapter;
	}

	public static void showIntent(Intent intent)
	{
		Log.i("Intent.action", getString(intent.getAction()));
		Log.i("Intent.dataString", getString(intent.getDataString()));
		Log.i("Intent.string", intent.toString());
		if (intent.getCategories() != null)
		{
			Log.i("Intent.categories", getString(intent.getCategories().toString()));
		}
		if (intent.getExtras() != null)
		{
			Log.i("Intent.extras", intent.getExtras().toString());
		}
	}

	private static String getString(String value) {
		return value!= null?value:"null";
	}
}